package GUIMain;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import storage.Employee;

import data.EmployeeInfo;
import employeeOperations.EmployeeTableModel;
import employeeOperations.NewEmployeeDialog;
import employeeOperations.NewLocationDialog;


/**
 * Will support two menus, first being Sales editor and second being CS Editor
 * @author lax116
 *
 */
public class EmployeeEditor {
	public static final int CS_EDITOR = 0;
	public static final int SALES_EDITOR = 1;
	
	private static JDialog dialog;
	private static JLabel leftPaneLabel;
	private static JLabel rightPaneLabel;
	private static JScrollPane leftScrollPane;
	private static JScrollPane rightScrollPane;
	private static JList leftList;
	private static DefaultListModel leftModel;
	private static JTable rightTable;
	private static JButton newLocButton;
	private static JButton removeLocButton;
	private static JButton newCSButton;
	private static JButton removeCSButton;
	private static JButton saveAndQuitButton;
	private static JButton quitButton;
	private static GridBagConstraints gbc;
	private static EmployeeTableModel rightModel;
	private static HashMap<String, ArrayList<Employee>> employeeMap;
	private static HashMap<String, Boolean> locationMap;
	private static String currentLocation;
	private static NewEmployeeDialog newEmployeeDialog;
	private static NewLocationDialog newLocationDialog;
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public static void open(int type){
		setup(type);
		GUIFocusManager.add(dialog);
	}
	/**
	 * Initializes all components and resets selections
	 */
	@SuppressWarnings("unchecked")
	public static void setup(int type){
		switch(type){
		case CS_EDITOR:
			employeeMap = (HashMap<String, ArrayList<Employee>>) EmployeeInfo.getEmployeeMap().clone();
			locationMap = (HashMap<String,Boolean>) EmployeeInfo.getLocationMap().clone();
			break;
		case SALES_EDITOR:
			employeeMap = (HashMap<String, ArrayList<Employee>>) EmployeeInfo.getSalesMap().clone();
			break;
		}
		newCSButton.setEnabled(false);
		removeCSButton.setEnabled(false);
		removeLocButton.setEnabled(false);
		leftList.setSelectedIndex(-1);
		rightModel.removeAllRows();
			updateLeftTable();
		updateRightTable();
	}
	/**
	 * @wbp.parser.entryPoint
	 */
	public static void initialize(){
		dialog = new JDialog();
		dialog.setTitle("Employee Editor");
		currentLocation = null;
		dialog.getContentPane().setLayout(new GridBagLayout());
		leftPaneLabel = new JLabel("Destinations");
		rightPaneLabel = new JLabel("Employees");
		leftModel = new DefaultListModel();
		leftList = new JList(leftModel);
		leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		leftScrollPane = new JScrollPane(leftList);
		newEmployeeDialog = new NewEmployeeDialog();
		newLocationDialog = new NewLocationDialog();
		rightModel = new EmployeeTableModel(true);
		rightTable = new JTable(rightModel);
		rightTable.getColumnModel().getColumn(0).setHeaderValue("Name");
		rightTable.getColumnModel().getColumn(1).setHeaderValue("Email");
		rightTable.getColumnModel().getColumn(2).setHeaderValue("Remove");
		rightScrollPane = new JScrollPane(rightTable);
		newLocButton = new JButton("Add Location");
		removeLocButton = new JButton("Remove Selected");
		newCSButton = new JButton("Add Employee");
		removeCSButton = new JButton("Remove Selected");
		removeCSButton.setEnabled(false);
		saveAndQuitButton = new JButton("Save");
		quitButton = new JButton("Cancel");
		gbc = new GridBagConstraints();
		
		gbc.insets.left = 4;
		gbc.insets.right = 4;
		gbc.insets.top = 4;
		gbc.insets.bottom = 4;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		int counter = 0;
		gbc.gridx =0;
		gbc.gridy = counter++;
		dialog.getContentPane().add(leftPaneLabel,gbc);
		gbc.gridx = 1;
		dialog.getContentPane().add(rightPaneLabel,gbc);

		gbc.gridy = counter++;
		gbc.gridheight = 3;
		gbc.gridx =0;
		leftScrollPane.setPreferredSize(new Dimension(250,400));
		dialog.getContentPane().add(leftScrollPane,gbc);
		gbc.gridx = 1; 
		rightScrollPane.setPreferredSize(new Dimension(450,400));
		dialog.getContentPane().add(rightScrollPane,gbc);
		gbc.gridheight = 1;
		
		JPanel destLocPanel = new JPanel();
		destLocPanel.setPreferredSize(new Dimension(250,40));
		destLocPanel.setLayout(new GridBagLayout());
		gbc.gridx =0;
		gbc.gridy =0;
		destLocPanel.add(newLocButton,gbc);
		gbc.gridx = 1;
		gbc.gridy =0;
		destLocPanel.add(removeLocButton,gbc);
		
		JPanel destCSPanel = new JPanel();
		destCSPanel.setPreferredSize(new Dimension(450,40));
		destCSPanel.setLayout(new GridBagLayout());
		gbc.gridx =0;
		gbc.gridy =0;
		destCSPanel.add(newCSButton,gbc);
		gbc.gridx = 1;
		destCSPanel.add(removeCSButton,gbc);
		
		gbc.gridx = 0;
		gbc.gridy = (counter += 3);
		dialog.getContentPane().add(destLocPanel,gbc);
		gbc.gridx = 1;
		dialog.getContentPane().add(destCSPanel,gbc);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(700,40));
		buttonPanel.setLayout(new GridBagLayout());
		gbc.gridx = 0; 
		gbc.gridy = 0;
		buttonPanel.add(quitButton,gbc);
		gbc.gridx = 1;
		buttonPanel.add(saveAndQuitButton,gbc);
		
		gbc.gridx = 0;
		gbc.gridwidth = 2;
		gbc.gridy = counter+=2;
		dialog.getContentPane().add(buttonPanel,gbc);
		
		attachListeners();
		
		GUIFocusManager.setDialogProperties(dialog);
	}
	private static void attachListeners(){
		quitButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		saveAndQuitButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//write all changes to file
				EmployeeInfo.setEmployeeMap(employeeMap);
				EmployeeInfo.setLocationMap(locationMap);
				employeeMap = null;
				EmployeeInfo.writeToFile();
				GUIFocusManager.remove();
			}
		});
		leftList.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				leftList.revalidate();
				//If a selection has been made active right panel options
				if(leftList.getSelectedIndex()>=0){
					//something is selected, activate employee buttons
					currentLocation = (String)getLocations()[leftList.getSelectedIndex()];
					newCSButton.setEnabled(true);
					removeLocButton.setEnabled(true);
					removeCSButton.setEnabled(true);
					//Load employee table with relevant information
					updateRightTable();
				}else{
					newCSButton.setEnabled(false);
					removeCSButton.setEnabled(false);
					removeLocButton.setEnabled(false);
				}
			}
		});
		newLocButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				newLocationDialog.showNewLocationDialog();
			}
		});
		newCSButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				newEmployeeDialog.showEmployeeInputDialog();
			}
		});
		removeCSButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				rightModel.removeAllSelected();
			}
		});
		removeLocButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){

				String location = (String) getLocations()[leftList.getSelectedIndex()];
				int selection = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove location: " +location+ "?" + " All associated employee records will be removed","Confirm Location Removal",JOptionPane.YES_NO_OPTION);
				switch(selection){
					case JOptionPane.YES_OPTION:
						employeeMap.remove(location);
						locationMap.remove(location);
						updateLeftTable();
						//Clear all entries from the right table
						rightModel.removeAllRows();
						break;
				}
			}
		});
	}
	/**
	 * Loads location data and refreshes employee data also
	 */
	private static void updateLeftTable(){
		leftModel.clear();
		for(int i=0;i<getLocations().length;i++){
			leftModel.add(i, getLocations()[i] + "  Origin: " + locationMap.get(getLocations()[i]));
			//(getLocations()[i]);
		}
		updateRightTable();
	}
	/**
	 * Loads employee data in right table
	 */
	private static void updateRightTable(){
		if(leftList.getSelectedIndex()!=-1){
			Object temp = getLocations()[leftList.getSelectedIndex()];
			rightModel.removeAllRows();
			
			if(temp!=null){
				String selectedLocation = (String)temp;
				ArrayList<Employee> empList = getEmployees(selectedLocation);
				
				for(int i=0;i<empList.size();i++){
					Vector<Object> newRow = new Vector<Object> (3);
					newRow.add(empList.get(i).name);
					newRow.add(empList.get(i).email);
					newRow.add(new Boolean(false));
					rightModel.addRow(newRow);
				}
			}
		}
	}
	/**
	 * Since the data between the employee table model and the GUI editor
	 * are in two difference locations, this function is called whenever
	 * a change is made in the GUI
	 * @param location
	 * @param empList
	 */
	public static void syncData(ArrayList<Employee> empList){
		employeeMap.put(currentLocation, empList);
	}
	private static ArrayList<Employee> getEmployees(String location){
		return employeeMap.get(location);
	}
	public static Object[] getLocations(){
		return employeeMap.keySet().toArray();
	}
	public static void addEmployeeToCurrentLocation(Employee e){
		employeeMap.get(currentLocation).add(e);
		updateRightTable();
	}
	public static boolean addNewLocation(String s,  Boolean isOrigin){
		if(locationMap.containsKey(s.toUpperCase())){
			return false;
		}else{
			employeeMap.put(s.toUpperCase(), new ArrayList<Employee>());
			locationMap.put(s.toUpperCase(), isOrigin);
			updateLeftTable();
			return true;
		}
	}
}
